package com.zhq.blog.manage.domain.controller.user;

import java.util.List;

import javax.validation.Valid;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.zhq.blog.core.domain.user.UserRank;
import com.zhq.blog.manage.domain.controller.BaseController;
import com.zhq.blog.manage.domain.controller.BossModel;

@Controller
@Scope("prototype")
@RequestMapping("/admin/user/userRank/*")
public class UserRankController extends BaseController {
	
	@RequestMapping()
    public BossModel index(){
		List<UserRank> userRanks = context.getUserRankRepository().findAll();
		UserRank userRank = new UserRank();
		userRank.setUserRankList(userRanks);
		return createModel("admin/user/userRank/list").add("userRank",userRank);
	}
	
	@RequestMapping(value="delete")
	public BossModel delete(long id){
		context.getUserRankRepository().delete(id);
		List<UserRank> userRanks = context.getUserRankRepository().findAll();
		UserRank userRank = new UserRank();
		userRank.setUserRankList(userRanks);
		return createModel("admin/user/userRank/userRank_table").add("userRank",userRank);
	}
	@RequestMapping(value="edit/{id}")
	public BossModel edit(@PathVariable long id){
		return createModel("admin/user/userRank/info").add("userRank",context.getUserRankRepository().findOne(id));
	}
	
	@RequestMapping(value="add")
	public BossModel add(){
		return createUserRankModel(new UserRank());
	}
	
	@RequestMapping(value="save",method = RequestMethod.POST)
	public BossModel save(@Valid final UserRank userRank, final BindingResult bindingResult, final ModelMap model, RedirectAttributes redirectUserRanks){
		if (bindingResult.hasErrors()) {
			return createModel("admin/user/userRank/info");
		}
		context.getUserRankRepository().save(userRank);
		String message = userRank.getSucessFlashMessage();
		redirectUserRanks.addFlashAttribute("message", message);
		return createModel("redirect:/admin/user/userRank/index");
	}
	
	@RequestMapping(value = "saveRank", method = RequestMethod.POST)
	public BossModel saveRank(UserRank userRank){
		List<UserRank> skuList = userRank.getUserRankList(); 
		for (int i = 0; i < skuList.size(); i++) {
			UserRank Rank = context.getUserRankRepository().findOne(skuList.get(i).getId());
			Rank.setName(skuList.get(i).getName());
			Rank.setExperienceMix(skuList.get(i).getExperienceMix());
			Rank.setExperienceMax(skuList.get(i).getExperienceMax());
			Rank.setStar(skuList.get(i).getStar());
			context.getUserRankRepository().save(Rank);
		}
		return createModel("admin/user/userRank/list").add("userRankPage",context.getUserRankRepository().findAll());
	}
	
	private BossModel createUserRankModel(UserRank userRank){
		BossModel modelAndView = createModel("admin/user/userRank/info");
		if(userRank != null)
			modelAndView.add("userRank",userRank);
		return modelAndView;
	}
	


}
